﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_TourDurationAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            ValidateControl();
            BindDataToLanguageDropDown();
            BindDataWhenEdit();
        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();

        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblDurationDescription.Text = MultipleLanguage.LBL_TOURDUARTION[nDefaultLanguage - 1];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
    }
    #region BindDataToDDL
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataWhenEdit();
    }
    #endregion

    #region Relative Functions
    private void ValidateControl()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        rfvDurationDescription.ErrorMessage = MessagesValidate.TourDuration_EMPTY[nDefaultLanguage - 1];
    }
    private void BindDataWhenEdit()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    TourDuration objTourDuration = new TourDuration();
                    objTourDuration.LoadById(nID);
                    switch (nCurrentLanguage)
                    {
                        case 1:
                            txtDurationDescription.Text = ConvertData.ConvertToString(objTourDuration.Data.DurationDescription1);
                            break;
                        case 2:
                            txtDurationDescription.Text = ConvertData.ConvertToString(objTourDuration.Data.DurationDescription2);
                            break;
                        case 3:
                            txtDurationDescription.Text = ConvertData.ConvertToString(objTourDuration.Data.DurationDescription3);
                            break;
                    }

                }
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDURATION;
            Response.Redirect(sLink);
        }
    }
    private eTourDuration SetData()
    {
        TourDuration objTourDuration = new TourDuration();
        eTourDuration eTourDuration = this.TourDurationEntity();
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLanguage)
        {
            case 1:
                objTourDuration.Data.DurationDescription1 = ConvertData.ConvertToString(txtDurationDescription.Text.Trim());

                objTourDuration.Data.DurationDescription2 = ConvertData.ConvertToString(eTourDuration.DurationDescription2);
                objTourDuration.Data.DurationDescription3 = ConvertData.ConvertToString(eTourDuration.DurationDescription3);
                break;
            case 2:
                objTourDuration.Data.DurationDescription2 = ConvertData.ConvertToString(txtDurationDescription.Text.Trim());

                objTourDuration.Data.DurationDescription1 = ConvertData.ConvertToString(eTourDuration.DurationDescription1);
                objTourDuration.Data.DurationDescription3 = ConvertData.ConvertToString(eTourDuration.DurationDescription3);
                break;
            case 3:
                objTourDuration.Data.DurationDescription3 = ConvertData.ConvertToString(txtDurationDescription.Text.Trim());

                objTourDuration.Data.DurationDescription1 = ConvertData.ConvertToString(eTourDuration.DurationDescription1);
                objTourDuration.Data.DurationDescription2 = ConvertData.ConvertToString(eTourDuration.DurationDescription2);
                break;
        }
        return objTourDuration.Data;
    }
    private eTourDuration TourDurationEntity()
    {
        eTourDuration entityTourDuration = new eTourDuration();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            TourDuration objTourDuration = new TourDuration();
            objTourDuration.LoadById(nItemID);
            entityTourDuration = objTourDuration.Data;
        }
        return entityTourDuration;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        TourDuration objTourDuration = new TourDuration();

        objTourDuration.Data = this.SetData();
        int nResult = 0;
        string sLink = "";
        string sAction = Request.QueryString[Config.ACTION];
        switch (sAction)
        {
            case Config.ACTION_ADD:
                nResult = objTourDuration.Insert();
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDURATION + "&" + Config.MESS_ID + "=1";
                    Response.Redirect(sLink);
                }
                else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                break;
            case Config.ACTION_EDIT:
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nResult = objTourDuration.Update(nID);
                if (nResult > 0)
                {
                    Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                }
                else
                    Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                break;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURDURATION);

    }
    #endregion
}